package com.woniuxy.test_map.test_treemap;

/**
 * description: TestTreeMap <br>
 * date: 2022/2/4 2:29 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

import java.util.Map;
import java.util.TreeMap;

/**
 * 测试TreeMap的使用
 */
public class TestTreeMap {
    public static void main(String[] args) {
        Map<Integer, String> treemap = new TreeMap<>();
        treemap.put(20, "aa");
        treemap.put(3, "b");
        treemap.put(13, "cc");

        //按照key递增1的方式排序
        for (Integer key : treemap.keySet()) {
            System.out.println(key + "---" + treemap.get(key));
        }

        Map<Emp, String> treemap2 = new TreeMap<>();
        treemap2.put(new Emp(1, "zs", 10000), "是个好员工");
        treemap2.put(new Emp(3, "ls", 1000), "工作不积极");
        treemap2.put(new Emp(2, "xs", 3000), "工作还不错");
    }

    static class Emp implements Comparable<Emp> {

        int id;
        String name;
        double salary;

        public Emp(int id, String name, double salary) {
            this.id = id;
            this.name = name;
            this.salary = salary;
        }

        @Override
        public String toString() {
            return "Emp{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", salary=" + salary +
                    '}';
        }

        @Override
        public int compareTo(Emp o) {
            //负数：小于，0：等于，正数：大于
            if (this.salary > o.salary) {
                return 1;
            } else if (this.salary < o.salary) {
                return -1;
            } else {
                if (this.id > o.id) {
                    return 1;
                } else if (this.id < o.id) {
                    return -1;
                } else {
                    return 0;
                }
            }
        }
    }
}
